﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;

namespace PCS.Utilities.Web.Controls
{
    public class LinkPager : Control
    {
        private string _class = string.Empty;
        private int totalRecord = 0;
        private int pageSize = 0;
        private string baseUrl = string.Empty;

        public LinkPager() { }

        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder output = new StringBuilder();
            if (PageIndex > 1)
            {
                output.AppendFormat("<a href=\"{0}{1}\">{2}</a> | ", Url, AddPageQueryString(1), this.First);
                output.AppendFormat("<a href=\"{0}{1}\">{2}</a> | ", Url, AddPageQueryString(PageIndex - 1), this.Prev);
            }
            if (NumPage > 1)
            {
                for (int i = 1; i <= NumPage; i++)
                {
                    if (i == PageIndex)
                    {
                        output.AppendFormat("<a class=\"Active\" href=\"#\" onclick=\"return false;\">{0}</a> | ", i);
                    }
                    else
                    {
                        output.AppendFormat("<a href=\"{0}{1}\">{2}</a> | ", Url, AddPageQueryString(i), i);
                    }
                }
            }
            if (PageIndex < NumPage && NumPage > 1)
            {
                output.AppendFormat("<a href=\"{0}{1}\">{2}</a> | ", Url, AddPageQueryString(PageIndex + 1), this.Next);
                output.AppendFormat("<a href=\"{0}{1}\">{2}</a>", Url, AddPageQueryString(NumPage), this.Last);
            }
            writer.Write(output.ToString());
        }

        public string Class
        {
            get { return _class; }
            set { _class = value; }
        }
        public int TotalRecord
        {
            get { return totalRecord; }
            set { totalRecord = value; }
        }
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        public string BaseUrl
        {
            get { return baseUrl; }
            set { baseUrl = value; }
        }

        private string first;
        public string First
        {
            get { return first; }
            set { first = value; }
        }

        private string next;
        public string Next
        {
            get { return next; }
            set { next = value; }
        }

        private string last;
        public string Last
        {
            get { return last; }
            set { last = value; }
        }

        private string prev;
        public string Prev
        {
            get { return prev; }
            set { prev = value; }
        }

        public int PageIndex
        {
            get
            {
                int pageIndex = 0;
                int.TryParse(HttpContext.Current.Request["page"], out pageIndex);
                return pageIndex == 0 ? 1 : pageIndex;
            }
        }

        public int NumPage
        {
            get
            {
                if (pageSize <= 0)
                    return 0;
                return (int)Math.Ceiling((decimal)totalRecord / (decimal)pageSize);
            }
        }

        private string AddPageQueryString(int page)
        {
            return Url.Contains("?") ? string.Format("&page={0}", page) : string.Format("?page={0}", page);
        }

        private string Url
        {
            get
            {
                string url = baseUrl != string.Empty ? baseUrl : Context.Request.RawUrl;
                if (url.Contains("?page"))
                {
                    url = url.Substring(0, url.IndexOf("?page"));
                }
                if (url.Contains("&page"))
                {
                    url = url.Substring(0, url.IndexOf("&page"));
                }
                return url;
            }
        }
    }
}
